Sorting Algorithms Comparison

PolyU DSAI2201 Lecture 13 2025-11-25

Comparison Overview: Core Sorting Methods

Sorting algorithms are fundamental to computer science, often defining the efficiency baseline for large data sets. The choice between Merge Sort, Bubble Sort, and Heap Sort depends crucially on requirements for stability, auxiliary space usage, and predictable performance across various input distributions.

Key Characteristics

  • Merge Sort: A stable, Divide and Conquer approach. Its primary drawback is its requirement for O(N)O(N) auxiliary space, often necessary for the merging step.
  • Bubble Sort: The simplest comparison sort. While easy to implement and requiring only O(1)O(1) auxiliary space, its quadratic worst-case performance makes it impractical for large datasets.
  • Heap Sort: Utilizes the structure of a Max/Min Heap (a priority queue structure) to perform selection in O(logN)O(\log N) time. It offers optimal time complexity *and* excellent space efficiency (O(1)O(1) auxiliary space), though it is generally unstable.

Complexity and Stability Comparison

Algorithm Best Case Time Average Case Time Worst Case Time Auxiliary Space Stable?
Bubble Sort O(N)O(N) O(N2)O(N^2) O(N2)O(N^2) O(1)O(1) Yes
Merge Sort O(NlogN)O(N \log N) O(NlogN)O(N \log N) O(NlogN)O(N \log N) O(N)O(N) Yes
Heap Sort O(NlogN)O(N \log N) O(NlogN)O(N \log N) O(NlogN)O(N \log N) O(1)O(1) No
📝 Interactive Quiz

1. Which algorithm is most efficient when minimizing auxiliary space *and* guaranteeing O(NlogN)O(N \log N) worst-case performance?

  • A) Bubble Sort
  • B) Merge Sort
  • C) Heap Sort
  • D) Quick Sort

2. If you must preserve the relative order of equal elements (stability), which of these algorithms is a suitable choice?

  • A) Heap Sort
  • B) Merge Sort
  • C) Both Heap Sort and Merge Sort
  • D) Neither are stable

3. Which algorithm is known for its O(N)O(N) auxiliary space requirement, making it less ideal for memory-constrained environments?

  • A) Bubble Sort
  • B) Merge Sort
  • C) Heap Sort